package com.caits.lbs.framework.services.mina;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.caits.lbs.framework.log.CommonLogFactory;
 
/**
 * <p>ServerIoHandler</p>
 * <p>nio服务器段消息处理类</p>
 *
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version		0.0.0
 * <table style="border:1px solid gray;">
 * <tr>
 * <th width="100px">版本号</th><th width="100px">动作</th><th width="100px">修改人</th><th width="100px">修改时间</th>
 * </tr>
 * <!-- 以 Table 方式书写修改历史 -->
 * <tr>
 * <td>0.0.0</td><td>创建类</td><td>zhouhuabin</td><td>2014年9月25日 下午3:22:13</td>
 * </tr>
 * <tr>
 * <td>XXX</td><td>XXX</td><td>XXX</td><td>XXX</td>
 * </tr>
 * </table>
*/
public class ServerIoHandler extends IoHandlerAdapter {
	private final Logger logger = CommonLogFactory.getLog();
 
	//出现异常的时候调用.
	public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
		logger.warn("Unexpected exception.", cause);  
    }
   //接收客户端新的消息的时候调用.
    public void messageReceived(IoSession session, Object obj) throws Exception {
        //收到的信息
        logger.info("收到的信息:"+session.toString());
        session.write("Server Send: " + obj.toString());
        logger.debug("return the state to the client successfull......");
    }

    public void messageSent(IoSession session, Object pArg1) throws Exception {
    	//session.write("通知客户端已经收到信息！！！");
    }
 
    public void sessionClosed(IoSession session) throws Exception {
    	logger.info("sessionClosed..."+session.toString());
    }
    //当一个客户端连接到服务器的时候被调用.
    public void sessionCreated(IoSession session) throws Exception {
    	logger.info("Session created..."+session.toString());
    }
    //会话空闲
    public void sessionIdle(IoSession session, IdleStatus pArg1) throws Exception {
    	logger.info("sessionIdle ..."+session.toString());
    }
    //打开会话，与sessionCreated最的区别是它是从另一个线程处调用的。
    public void sessionOpened(IoSession session) throws Exception {
        session.write("[Server: Client,I'm server.]");
    }
 
}
